﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace BuddHa.Common.JsonOpex
{
    public class JsonOpexSerializer : IJsonOpexSerializer
    {
        private readonly JsonSerializerOptions _jsonSerializerOptions;
        public JsonOpexSerializer(Action<JsonSerializerOptions> action)
        {
            _jsonSerializerOptions = ConstProperty.CreateDefaultJsonSerializerOptions();
            action?.Invoke(_jsonSerializerOptions);
        }

        public T Deserialize<T>(string json)
        {
            return System.Text.Json.JsonSerializer.Deserialize<T>(json, _jsonSerializerOptions);
        }

        public T Deserialize<T>(ReadOnlySpan<byte> utf8Json)
        {
            return System.Text.Json.JsonSerializer.Deserialize<T>(utf8Json, _jsonSerializerOptions);
        }

        public object Deserialize(string json, Type type)
        {
            return System.Text.Json.JsonSerializer.Deserialize(json, type);

        }

        public string Serialize<T>(T obj)
        {
            return System.Text.Json.JsonSerializer.Serialize<T>(obj, _jsonSerializerOptions);
        }

        public byte[] SerializeToUtf8Bytes<T>(T obj)
        {
            return System.Text.Json.JsonSerializer.SerializeToUtf8Bytes<T>(obj, _jsonSerializerOptions);
        }
    }
}
